const express = require("express");
const User = require("../models/User");
const router = express.Router();

router.get("/", async (req, res) => {
  res.status(200).send("welcome");
});

router.post("/createUser", async (req, res) => {
  //Create new user
  try {
    const user = new User(req.body);
    await user.save();
    res.status(201).send({ user });
  } catch (error) {
    res.status(400).send({ error });
  }
});

router.post("/userLogin", async (req, res) => {
  try {
    const { email, password } = req.body;
    const user = await User.findByCredentials(email, password);
    if (!user) {
      return res.status(401).send({ error: "密码错误" });
    }
    res.status(200).send({ user });
  } catch (error) {
    res.status(400).sent({ error });
  }
});

module.exports = router;
